package csc.fresher.view;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.model.DataModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.entity.SystemUser;
import csc.fresher.service.SystemUserChangeStateService;
import csc.fresher.service.SystemUserService;

@Component
@Scope("session")
public class SystemUserApprove implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private SystemUserChangeStateService systemUserChangeStateService;
	
	@Autowired
	private SystemUserService systemUserService;
	
	@Autowired
	private SystemLoginManageBean systemLoginManageBean;

	private List<SystemUser> systemUserList;
	private SystemUserDataModel systemUserDataModel;
	private SystemUser systemUser;
	private Boolean selectAll;
	private Map<Long, Boolean> selectedCheckbox = new HashMap<Long, Boolean>();
	private List<SystemUser> selectedSystemUsers;

	@PostConstruct
	public void init() {
		try {
			this.systemUser = null;
			this.systemUserList = firstLoad();
			this.systemUserDataModel = new SystemUserDataModel(systemUserList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//first load
		public List<SystemUser> firstLoad() {
			systemUserList = systemUserService.getSystemUserByState("new");
			return systemUserList;
		}
	
	//APPROVE button
	public String approve() {
			for(SystemUser s : selectedSystemUsers) {

					SystemUser systemUser = new SystemUser();
					systemUser.setSystemUserID(s.getSystemUserID());
					systemUserChangeStateService.changeSystemUserStateToActive(systemUser);
					systemUserChangeStateService.updateApprovedByID(systemUser, systemLoginManageBean.getSystemUser().getSystemUserID());
					
				
			}
			systemUserList = systemUserService.getSystemUserByState("new");
			this.systemUserDataModel = new SystemUserDataModel(systemUserList);
			
		return "approveUser";
	}
	
	public boolean checkNull() {
		boolean flag = false;
		for(SystemUser s : systemUserList) {							
			if(s != null) {
				flag = true;
			}
		}
	return flag;
}


	public SystemUserService getSystemUserService() {
		return systemUserService;
	}

	public void setSystemUserService(SystemUserService systemUserService) {
		this.systemUserService = systemUserService;
	}

	public List<SystemUser> getSystemUserList() {
		return systemUserList;
	}

	public void setSystemUserList(List<SystemUser> systemUserList) {
		this.systemUserList = systemUserList;
	}


	public DataModel<SystemUser> getSystemUserDataModel() {
		return systemUserDataModel;
	}

	public void setSystemUserListDataModel(
			SystemUserDataModel systemUserDataModel) {
		this.systemUserDataModel = systemUserDataModel;
	}

	public SystemUser getSystemUser() {
		return systemUser;
	}

	public void setSystemUser(SystemUser systemUser) {
		this.systemUser = systemUser;
	}

	public Boolean getSelectAll() {
		return selectAll;
	}

	public void setSelectAll(Boolean selectAll) {
		this.selectAll = selectAll;
	}

	public Map<Long, Boolean> getSelectedCheckbox() {
		return selectedCheckbox;
	}

	public void setSelectedCheckbox(Map<Long, Boolean> selectedCheckbox) {
		this.selectedCheckbox = selectedCheckbox;
	}



	public List<SystemUser> getSelectedSystemUsers() {
		return selectedSystemUsers;
	}

	public void setSelectedSystemUsers(List<SystemUser> selectedSystemUsers) {
		this.selectedSystemUsers = selectedSystemUsers;
	}
	
}
